package com.cookie.framework.helper;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.framework.helper
 * 文件名:    WindowHelper
 * 创建者:    cookie
 * 创建时间:   2020/5/17 2:43
 * 描述:      window的辅助类
 */

public class WindowHelper {

    private static volatile WindowHelper mInstance = null;
    private WindowManager wm;
    private WindowManager.LayoutParams lp;
    private Context mContext;

    private WindowHelper() {
    }

    public static WindowHelper getInstance() {
        if (mInstance == null) {
            synchronized (WindowHelper.class) {
                if (mInstance == null) {
                    mInstance = new WindowHelper();
                }
            }
        }
        return mInstance;
    }

    //初始化Window
    public void initWindow(Context context) {
        mContext = context;
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        lp = createLayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                Gravity.CENTER
        );

    }

    public WindowManager.LayoutParams createLayoutParams(int width, int height, int gravity) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();

        //设置宽高属性
        layoutParams.width = width;
        layoutParams.height = height;

        //设置标志位
        layoutParams.flags =
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

        //设置格式
        layoutParams.format = PixelFormat.TRANSLUCENT;
        //设置位置
        layoutParams.gravity = gravity;

        //设置类型
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        return layoutParams;
    }

    //创建view的视图
    public View getView(int layoutId) {
        return View.inflate(mContext, layoutId, null);
    }

    //显示窗口
    public void showView(View view) {
        if (view != null) {
            if (view.getParent() == null) {
                wm.addView(view, lp);
            }
        }
    }

    //自定义属性
    public void showView(View view, WindowManager.LayoutParams layoutParams) {
        if (view != null) {
            if (view.getParent() == null) {
                wm.addView(view, layoutParams);
            }
        }
    }

    //隐藏视图
    public void hideView(View view) {
        if (view != null) {
            if (view.getParent() != null) {
                wm.removeView(view);
            }
        }
    }

    //更新view
    public void  updateView(View view, WindowManager.LayoutParams layoutParams) {
        if (view!=null && layoutParams!=null){
            wm.updateViewLayout(view,layoutParams);
        }
    }

}
